Skip to main content

Hello World

Step 0: Create a Boilerplate Spring Boot Project with Maven​

a pom.xml file
  • <groupId> element
    • the text in this xml element is set via Spring Initializr
  • <artifactId> element
    • the text in this xml element is set via Spring Initializr
  • <version> element
    • eg. 0.0.1-SNAPSHOT means it is not a 1.0 release version, but a WIP snapshot version
  • <properties> element
    • <java.version> element
      • the java version you picked in Spring Initializr
  • <dependencies> (plural) element
    • list of multiple <dependency> (singular) elements, each at least with a <groupId> and <artifactId> element
      • spring-boot-starter-data-jdbc (to access databases)
      • spring-boot-starter-web (web dependency with an embedded web server)
      • h2 (h2 database)
      • spring-boot-starter-test (for testing)
  • if we need to add some more dependencies later, we will put them in the pom.xml
mvnw (for Linux) and mvnw.cmd (for Windows)
  • a Maven wrapper
  • the Spring project comes with an embedded Maven version, so you do not need to download Maven
  • with this you can run all the Maven commands you want
src
  • (src has a standard Maven directory structure)
  • main
    • java
      • com.companyname.phth.photoz.clone (all java classes will live in this folder)
        • PhotozCloneApplication (a java class that the Spring Initializr has put there)
    • resources
      • application.properties (to configure the Spring Application)
      • static (if we want to write static html pages)
        • "static folder is used for serving web static content, all the css, js, html etc. (also default folder search by spring) docs.spring.io", stackoverflow
      • templates (if we want to write dynamic html pages)
        • "templates folder is a place where you put all the thymeleaf templates. It is a default directory (by default spring will look inside for any templates).", stackoverflow
  • test
    • java
      • com.companyname.phth.photoz.clone
        • PhotozCloneApplicationTests (a test class)

Step 1: Boot up the Boilerplate Spring Boot Project​

  • the PhotozCloneApplication class has one method, the main method
    • the main() method runs the Spring specific function SpringApplication.run() and puts your class PhotozCloneApplication.class inside that SpringApplication.run() method that it executes
    • SpringApplication.run() boots up Spring Boot which means booting up the embedded web server and after that we should be able to see our page in the browser
      • this will log the following messages in the output window ("Run" window):
        • Starting service [Tomcat] which means your webserver is being booted up
        • Tomcat started on port 8080
PhotozCloneApplication.java
package com.companyname.phth.photoz.clone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PhotozCloneApplication {

public static void main(String[] args) {
SpringApplication.run(PhotozCloneApplication.class, args);
}

}
  • this boilerplate Spring Boot project will only show an error (type=Not Found, status=404)
    • because there is nothing to accept JSON messages or to send it back to the frontend ("REST API")

Step 2: Display a "Hello World" Message​

  • to get rid of the error message error (type=Not Found, status=404) we build a Hello World page
    • for this we create a new class (via alt insert in the file explorer) called PhotozController inside the base package com.companyname.phth.photoz.clone
PhotozController.java
package com.companyname.phth.photoz.clone;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class PhotozController {

public String hello() {
return "Hello World!";
}
}
  • problem: Spring does not understand what this PhotozController class is supposed to do, so we have to add the following annotations:
info

many things in Spring Boot work with annotations as a sort of marker to tell Spring that it has to do something special with that class

tip

You just have to start typing @someAnnotation and then press Enter on one of the suggested annotations. IntelliJ will automatically import the right class.

PhotozController.java
package com.companyname.phth.photoz.clone;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PhotozController {

public String hello() {
return "Hello World!";
}
}
  • now, when you start the Spring application Spring will look at all the classes inside the package (and also in all the subpackages) of the PhotozCloneApplication class and it will look for any REST controllers there.
    • if Spring finds REST controllers it will create a new instance of PhotozController for the user, ie. for the application developer
  • problem: the methods/functions in REST controllers are special, Spring does not understand what these functions are supposed to do, so we need to tell Spring what to do with the hello() method
    • more specifically, we need to map the hello() method, "mapping" means
      • The user/the browser will send HTTP GET requests to our server. The path will be eg. "/" (if we just want to a localhost:8080/).
      • we want to respond to that GET request.
  • with the @GetMapping("/") annotation, effectively, we are saying that the REST controller PhotozController has one method hello() which should be executed by Spring whenever a request hits the endpoint "/" with the HTTP GET method
PhotozController.java
package com.companyname.phth.photoz.clone;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PhotozController {

@GetMapping("/")
public String hello() {
return "Hello World!";
}
}
info

You can also have the @PostMapping, @DeleteMapping, etc. if you have different HTTP methods involved.

warning

by default, Spring does not automatically reload and register these new methods, so you have to stop and run the application again (or just "rerun" the application)

Summary: What we have just learned is that, if you only want to build the smallest possible "Hello World" page, it is enough to create one REST controller. When you boot up Spring, Spring is able to understand what you want. We want it to just execute the method and return the string "Hello World" whenever someone sends a GET request.